import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RegisterFrame extends JFrame {
    private JTextField accountField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JTextField nameField;
    private JComboBox<String> genderComboBox;
    private JTextField phoneNumberField;
    private JTextField birthDateField;
    private JComboBox<String> privacyOptionComboBox;

    public RegisterFrame() {
        setTitle("MBTI职业性格测试 - 注册");
        setSize(350, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(10, 2));

        JLabel accountLabel = new JLabel("账号:");
        panel.add(accountLabel);

        accountField = new JTextField();
        panel.add(accountField);

        JLabel passwordLabel = new JLabel("密码:");
        panel.add(passwordLabel);

        passwordField = new JPasswordField();
        panel.add(passwordField);

        JLabel confirmPasswordLabel = new JLabel("确认密码:");
        panel.add(confirmPasswordLabel);

        confirmPasswordField = new JPasswordField();
        panel.add(confirmPasswordField);

        JLabel nameLabel = new JLabel("姓名:");
        panel.add(nameLabel);

        nameField = new JTextField();
        panel.add(nameField);

        JLabel genderLabel = new JLabel("性别:");
        panel.add(genderLabel);

        genderComboBox = new JComboBox<>(new String[]{"男", "女"});
        panel.add(genderComboBox);

        JLabel phoneNumberLabel = new JLabel("手机号:");
        panel.add(phoneNumberLabel);

        phoneNumberField = new JTextField();
        panel.add(phoneNumberField);

        JLabel birthDateLabel = new JLabel("出生日期（格式：yyyy-MM-dd）:");
        panel.add(birthDateLabel);

        birthDateField = new JTextField();
        panel.add(birthDateField);

        JLabel privacyOptionLabel = new JLabel("测试结果隐私选项:");
        panel.add(privacyOptionLabel);

        privacyOptionComboBox = new JComboBox<>(new String[]{"是", "否"});
        panel.add(privacyOptionComboBox);

        JButton registerButton = new JButton("注册");
        panel.add(registerButton);

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String account = accountField.getText();
                String password = new String(passwordField.getPassword());
                String confirmPassword = new String(confirmPasswordField.getPassword());
                String name = nameField.getText();
                String gender = (String) genderComboBox.getSelectedItem();
                String phoneNumber = phoneNumberField.getText();
                String birthDateStr = birthDateField.getText();
                Date birthDate = null;
                try {
                    birthDate = new SimpleDateFormat("yyyy-MM-dd").parse(birthDateStr);
                } catch (ParseException ex) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "出生日期格式错误！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                String privacyOption = (String) privacyOptionComboBox.getSelectedItem();

                // 检查账号是否为空
                if (account.isEmpty()) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "账号不能为空，请输入账号！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                // 检查密码是否为空
                if (password.isEmpty()) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "密码不能为空，请输入密码！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                // 检查确认密码是否为空
                if (confirmPassword.isEmpty()) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "确认密码不能为空，请输入确认密码！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                // 检查姓名是否为空
                if (name.isEmpty()) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "姓名不能为空，请输入姓名！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                // 检查手机号是否为空
                if (phoneNumber.isEmpty()) {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "手机号不能为空，请输入手机号！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (password.equals(confirmPassword)) {
                    User user = new User();
                    user.setAccount(account);
                    user.setPIN(password);
                    user.setUtype("testuser"); // 假设注册为测试用户类型

                    UserDAO userDAO = new UserDAOImpl();
                    try {
                        userDAO.save(user);
                    } catch (IllegalArgumentException ex) {
                        if (ex.getMessage().equals("PIN不能为空")) {
                            JOptionPane.showMessageDialog(RegisterFrame.this, "密码不能为空，请重新输入！", "错误", JOptionPane.ERROR_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(RegisterFrame.this, "注册用户时出现错误，请稍后重试！", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                        return;
                    }

                    TestUser testUser = new TestUser();
                    testUser.setAccount(account);
                    testUser.setTname(name);
                    testUser.setTsex(gender);
                    testUser.setTnumber(phoneNumber);
                    testUser.setTbirth(birthDate);
                    testUser.setTprivacy(privacyOption);

                    TestUserDAO testUserDAO = new TestUserDAOImpl();
                    try {
                        testUserDAO.save(testUser);
                    } catch (IllegalArgumentException ex) {
                        if (ex.getMessage().equals("关联的账号不存在，请先创建账号。")) {
                            JOptionPane.showMessageDialog(RegisterFrame.this, "注册账号不存在，请先创建账号！", "错误", JOptionPane.ERROR_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(RegisterFrame.this, "注册测试用户时出现错误，请稍后重试！", "错误", JOptionPane.ERROR_MESSAGE);
                        }
                        return;
                    }

                    JOptionPane.showMessageDialog(RegisterFrame.this, "注册成功！");
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(RegisterFrame.this, "密码和确认密码不一致！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        add(panel);
    }
}